package com.ykt.service;

import com.jfinal.aop.Enhancer;
import com.ykt.cache.CameraCache;
import com.ykt.common.utils.PageUtil;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;
import com.ykt.model.Camera;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 摄像头服务
 * @author long.hua
 * @version 1.0.0
 * @since 2016-01-09 22:21
 */
public class CameraService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CameraService.class);

    private Camera cameraDao = Camera.dao;
    private CameraCache cameraCache = Enhancer.enhance(CameraCache.class);


    public Camera findById(int cameraTableId){
        return cameraDao.findById(cameraTableId);
    }

    /**
     * 根据商铺ID查询它所有的摄像头
     * @param param 商铺ID 和 分页参数
     * @return 摄像头信息
     */
    public PageData<Camera> findByMarketId(PageParam<Integer> param){
        PageData<Camera> page = cameraCache.findByMarketIdFromCache(param);
        if(!PageUtil.isNotEmpty(page)){

            page = findByMarketIdFromDB(param);
            cameraCache.saveByShopIdFromCache(param.getP(), page.getPageData());
        }

        return page;
    }

    /**
     * 根据商铺ID查询它所有的摄像头
     * @param param 商铺ID 和 分页参数
     * @return 摄像头信息
     */
    public PageData<Camera> findByShopId(PageParam<Integer> param){
        PageData<Camera> page = cameraCache.findByShopIdFromCache(param);
        if(!PageUtil.isNotEmpty(page)){

            page = findByShopIdFromDB(param);
            cameraCache.saveByShopIdFromCache(param.getP(), page.getPageData());
        }

        return page;
    }

    private PageData<Camera> findByShopIdFromDB(PageParam<Integer> param){
        return cameraDao.pageFindTinyCameraByShopId(param);
    }

    private PageData<Camera> findByMarketIdFromDB(PageParam<Integer> param){
        return cameraDao.pageFindTinyCameraByMarketId(param);
    }
}
